import { createRouter, createWebHistory } from "vue-router";
import type { RouteRecordRaw } from "vue-router";
import { setupRouterGuards } from "./guards";

// 基础路由配置
const routes: RouteRecordRaw[] = [
  // 登录相关路由
  {
    path: "/login",
    name: "login",
    component: () => import("../views/Login.vue"),
    meta: {
      title: "登录",
      hidden: true,
      requiresAuth: false
    }
  },
  {
    path: "/reg",
    name: "reg", 
    component: () => import("../views/reg.vue"),
    meta: {
      title: "注册",
      hidden: true,
      requiresAuth: false
    }
  },
  
  // 主应用路由
  {
    path: "/",
    name: "home",
    component: () => import("../views/Home.vue"),
    meta: {
      title: "首页",
      icon: "HomeOutlined",
      requiresAuth: true,
      breadcrumb: true
    },
    children: [
      {
        path: "",
        name: "main",
        component: () => import("../components/MainPage.vue"),
        meta: {
          title: "数据总览",
          icon: "DashboardOutlined",
          requiresAuth: true,
          breadcrumb: true,
          keepAlive: true,
          order: 1
        }
      },
      {
        path: "main",
        name: "mainPage",
        component: () => import("../components/MainPage.vue"),
        meta: {
          title: "数据总览",
          icon: "DashboardOutlined",
          requiresAuth: true,
          breadcrumb: true,
          keepAlive: true,
          order: 1
        }
      },
      {
        path: "userManagement",
        name: "userManagement",
        component: () => import("../components/UserManagement.vue"),
        meta: {
          title: "用户管理",
          icon: "UserOutlined",
          requiresAuth: true,
          roles: ["admin"],
          permissions: ["user:read"],
          breadcrumb: true,
          keepAlive: true,
          order: 2
        }
      },
      {
        path: "userProfile",
        name: "userProfile",
        component: () => import("../components/userProfile.vue"),
        meta: {
          title: "用户资料",
          icon: "ProfileOutlined",
          requiresAuth: true,
          breadcrumb: true,
          keepAlive: true,
          order: 3
        }
      },
      {
        path: "surveyList",
        name: "surveyList",
        component: () => import("../components/surveyList.vue"),
        meta: {
          title: "问卷列表",
          icon: "FileTextOutlined",
          requiresAuth: true,
          roles: ["admin", "user"],
          permissions: ["survey:read"],
          breadcrumb: true,
          keepAlive: true,
          order: 4
        }
      },
      {
        path: "addSurvey",
        name: "addSurvey",
        component: () => import("../components/addSurvey.vue"),
        meta: {
          title: "新增问卷",
          icon: "PlusOutlined",
          requiresAuth: true,
          roles: ["admin", "user"],
          permissions: ["survey:create"],
          breadcrumb: true,
          keepAlive: true,
          order: 5
        }
      },
      {
        path: "discussionList",
        name: "discussionList",
        component: () => import("../components/discussList.vue"),
        meta: {
          title: "讨论列表",
          icon: "MessageOutlined",
          requiresAuth: true,
          roles: ["admin", "user"],
          permissions: ["discussion:read"],
          breadcrumb: true,
          keepAlive: true,
          order: 6
        }
      }
    ]
  },
  
  // 问卷编辑路由
  {
    path: "/editQuestionare/:id?",
    name: "editQuestionare",
    component: () => import("../questionare/editQuestionare.vue"),
    meta: {
      title: "问卷编辑",
      icon: "EditOutlined",
      requiresAuth: true,
      roles: ["admin", "user"],
      permissions: ["survey:edit"],
      breadcrumb: true,
      keepAlive: true
    }
  },
  
  // 错误页面
  {
    path: "/403",
    name: "403",
    component: () => import("../views/403.vue"),
    meta: {
      title: "403 - 无权限",
      hidden: true,
      requiresAuth: false
    }
  },
  {
    path: "/404",
    name: "404",
    component: () => import("../views/404.vue"),
    meta: {
      title: "404 - 页面不存在",
      hidden: true,
      requiresAuth: false
    }
  },
  
  // 通配符路由，必须放在最后
  {
    path: "/:catchAll(.*)",
    name: "notFound",
    redirect: "/404"
  }
];

// 创建路由实例
const router = createRouter({
  history: createWebHistory(),
  routes,
  scrollBehavior(_, __, savedPosition) {
    if (savedPosition) {
      return savedPosition;
    } else {
      return { top: 0 };
    }
  }
});

// 设置路由守卫
setupRouterGuards(router);

export default router;